package com.sjgtw.cloud.web.rainfall.controller;

import com.sjgtw.cloud.common.core.utils.poi.ExcelUtil;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.web.rainfall.domain.RainfallWarning;
import com.sjgtw.cloud.web.rainfall.service.IRainfallWarningService;
import com.sjgtw.cloud.web.rainfall.vo.RainfallVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 降雨预警Controller
 * 
 * @author lvyoufang
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/rainfall")
@Api(tags = "4.1、降雨预警")
public class RainfallWarningController extends BaseController
{
    @Autowired
    private IRainfallWarningService rainfallWarningService;

    /**
     * 查询降雨预警列表
     */
    @ApiOperation(value = "查询降雨预警列表")
    @GetMapping("/list")
    public TableDataInfo list(RainfallWarning rainfallWarning)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", LocalDate.now().atStartOfDay());
        rainfallWarning.setParams(params);
        startPage();
        List<RainfallWarning> list = rainfallWarningService.selectRainfallWarningList(rainfallWarning);
        return getDataTable(list);
    }

    /**
     * 查询降雨预警列表
     */
    @ApiOperation(value = "查询降雨预警列表(历史)")
    @GetMapping("/listHistory")
    public TableDataInfo listHistory(RainfallWarning rainfallWarning)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("endTime", LocalDate.now().atStartOfDay());
        rainfallWarning.setParams(params);
        startPage();
        List<RainfallVO> rainfallVOS = rainfallWarningService.selectRainfallList(rainfallWarning);
        return getDataTable(rainfallVOS);
    }

    /**
     * 导出降雨预警列表
     */
    @ApiOperation(value = "导出降雨预警列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, RainfallWarning rainfallWarning)
    {
        List<RainfallWarning> list = rainfallWarningService.selectRainfallWarningList(rainfallWarning);
        ExcelUtil<RainfallWarning> util = new ExcelUtil<RainfallWarning>(RainfallWarning.class);
	    try {
		    util.exportExcel(response, list, "降雨预警数据");
	    } catch (IOException e) {
		    e.printStackTrace();
	    }
    }

    /**
     * 获取降雨预警详细信息
     */
    @ApiOperation(value = "获取降雨预警详细信息")
    @GetMapping(value = "/getInfo")
    public TableDataInfo getInfo(RainfallWarning rainfallWarning)
    {
        startPage();
        List<RainfallVO> rainfallVOS = rainfallWarningService.selectRainfallList(rainfallWarning);
        return getDataTable(rainfallVOS);
    }

    /**
     * 新增降雨预警
     */
    @ApiOperation(value = "新增降雨预警")
    @PostMapping
    public AjaxResult add(@RequestBody RainfallWarning rainfallWarning)
    {
        return toAjax(rainfallWarningService.insertRainfallWarning(rainfallWarning));
    }

    /**
     * 修改降雨预警
     */
    @ApiOperation(value = "修改降雨预警")
    @PutMapping
    public AjaxResult edit(@RequestBody RainfallWarning rainfallWarning)
    {
        return toAjax(rainfallWarningService.updateRainfallWarning(rainfallWarning));
    }

    /**
     * 删除降雨预警
     */
    @ApiOperation(value = "删除降雨预警")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(rainfallWarningService.deleteRainfallWarningByIds(ids));
    }

    @ApiOperation(value = "下发")
    @PostMapping("/issue")
    public AjaxResult issue(@RequestBody RainfallWarning rainfallWarning)
    {
        return toAjax(rainfallWarningService.issue(rainfallWarning));
    }
}
